package cn.itcast.bos.service.transit.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.delivery.IWayBillRepository;
import cn.itcast.bos.dao.transit.ITransitRepository;
import cn.itcast.bos.domain.delivery.WayBill;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.service.transit.ITransitInfoService;

@Service
@Transactional
public class TransitInfiServiceImpl implements ITransitInfoService{
	@Autowired
	private IWayBillRepository wayBillRepository;
	
	@Autowired
	private ITransitRepository transitRepository;
	
	@Override
	public void createTransits(String wayBillIds) {
		if (StringUtils.isNotBlank(wayBillIds)) {
			String[] wayBillArray = wayBillIds.split(",");
			for (String wayBillId : wayBillArray) {
				WayBill wayBill = wayBillRepository.findOne(Integer.parseInt(wayBillId));
				System.out.println("xxx:" + wayBill.getSignStatus());
				if (wayBill.getSignStatus() == null) {
					wayBill.setSignStatus(1);
				}
				if (wayBill.getSignStatus() == 1) {
					TransitInfo info = new TransitInfo();
					info.setWayBill(wayBill);
					info.setStatus("出入库中转");
					transitRepository.save(info);
					
					//更改运单状态
					wayBill.setSignStatus(2); //派送中
				}
			}
		}
	}

	@Override
	public Page<TransitInfo> findPageData(Pageable pageable) {
		return transitRepository.findAll(pageable);
	}

}
